package com.phison.fenestrapro.dashboard.util;

import com.phison.fenestrapro.dashboard.data.Project;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Layout;

/**
 *
 * 
 * @author Niall O'Hara
 *
 */
public class GridFormBuilder implements GlobalConstants {

	private Project building;
	private BeanItem<Project> buildingItem;
	
	public GridFormBuilder(Project building, BeanItem<Project> buildingItem) {
		// TODO Auto-generated constructor stub
		this.building = building;
		this.buildingItem = buildingItem;
	}

	@SuppressWarnings("serial")
	public Layout buildFormLayout(Object[] fields, int type) {

		Layout formLayout = null;
		formLayout = new GridLayout();
		formLayout.setWidth("100%");
		Form formContents = null;
		
		switch (type) {
			case daylightInput:
				formContents = new Form() {
					GridLayout layout = new GridLayout(4, 1);
					{
						setImmediate(true);
						setWriteThrough(true);
						layout.setColumnExpandRatio(0, 2);
						layout.setColumnExpandRatio(1, 3);
						layout.setColumnExpandRatio(2, 3);
						layout.setColumnExpandRatio(3, 3);
						layout.setSpacing(false);
						layout.setMargin(false);
						layout.setWidth("95%");
						setLayout(layout);
						layout.addStyleName("daylightWindows");
					}
		
					@Override
					protected void attachField(Object propertyId, Field field) {
						layout.addComponent(field);
					}
				};
				break;
				
			case daylightOutput:
				formContents = new Form() {
					GridLayout layout = new GridLayout(5, 1);
					{
						setImmediate(true);
						setWriteThrough(true);
						layout.setColumnExpandRatio(0, 2);
						layout.setColumnExpandRatio(1, 3);
						layout.setColumnExpandRatio(2, 3);
						layout.setColumnExpandRatio(3, 3);
						layout.setColumnExpandRatio(4, 3);
						layout.setSpacing(false);
						layout.setMargin(false);
						layout.setWidth("95%");
						setLayout(layout);
						layout.addStyleName("daylightWindows");
					}
		
					@Override
					protected void attachField(Object propertyId, Field field) {
						layout.addComponent(field);
					}
				};
				break;
		}

		formContents.setFormFieldFactory(new MyFieldFactory(building));

		formContents.setItemDataSource(buildingItem);

		formContents.setVisibleItemProperties(fields);

		formLayout.addComponent(formContents);

		return formLayout;

	}

}
